<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<uni-search-bar @input="input" placeholder="搜索" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		<!-- 搜索建议 -->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="gotoGoodsDetail(item.goods_id)">
				<view class="goods-name" >{{item.goods_name}}</view>
				<uni-icons type="right"></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史-->
		<view class="search-history-box" v-else>
			<!-- 搜索历史的标题部分 -->
			<view class="search-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="20" @click="cleanHistory"></uni-icons>
			</view>
			
			<!-- 搜索历史的列表展示 -->
			<view class="search-list">
				<uni-tag :text="item" v-for="(item, index) in histories" :key="index" circle="true" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//做防抖处理
				timer: null,
				keyword: " ",
				
				searchResults: [],
				searchHistory: ['a','testb', '奶茶']
			};
		},
		onLoad() {
			this.searchHistory = JSON.parse(uni.getStorageSync("keyword") || '[]');
		},
		
		methods: {
			input(e) {
				//如果不到500毫秒就清除之前的timer,已达到防抖动的效果
				clearTimeout(this.timer)
				
				this.timer = setTimeout(() => {
					//记录
					this.keyword = e;
					this.getSearchList();
				},500)	
			},
			
			getSearchList() {
				//如果在搜索框中输入的关键字为空
				// console.log(this.keyword)
				if (this.keyword.length === 0) {
					this.searchResults = [];
					return
				}
				this.searchResults = [
					{   
						"goods_id": 0,
						"goods_name": "test1",
					},
					{
						"goods_id": 1,
						"goods_name": "1. test-----------111111111----------------为了测试而写的",
					},
					{
						"goods_id": 2,
						"goods_name": "2. test----------2222222222222222------------为了测试而写的",
					},
					{
						"goods_id": 3,
						"goods_name": "3. test------------3333333333333333---------------为了测试而写的",
					},
					{
						"goods_id": 4,
						"goods_name": "4. test------------4444444444444444444---------------为了测试而写的",
					}
				]
			}
			,
			gotoGoodsDetail(goods_id) {
				this.saveSearchKeyWord();
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
				})
			},
			saveSearchKeyWord() {
				// this.searchHistory.push(this.keyword)
				// 使用集合解决关键字重复问题
				const set = new Set(this.searchHistory);
				set.add(this.keyword);
				//将对应的集合转换为数组
				this.searchHistory = Array.from(set)
				
				// 将数据持久化到本地
				uni.setStorageSync("keyword", JSON.stringify(this.searchHistory))
			},
			// 清除历史
			cleanHistory() {
				this.searchHistory = [],
				uni.setStorageSync("keyword", '[]')
			},
			gotoGoodsList(keyword) {
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query=' + keyword
				})
			}
		},
		computed: {
			histories() {
				return [...this.searchHistory].reverse()
			}
		}
	}
</script>

<style lang="scss">
.search-box { 
	position: sticky;
	//距离顶部的位置
	top: 0;
	
	// 提高层级,达到吸顶的效果
	z-index: 999;
	
}
.sugg-list {
	padding: 0 35px;
	.sugg-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 13px 0;
		font-size: 13px;
		border-bottom: 1px solid #d1d5cf;
		.goods-name {
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
	}
}

.search-history-box {
	padding: 0 25px;

	.search-title {
		display: flex;
		justify-content: space-between;
		height: 50px;
		align-items: center;
		font-size: 13px;
		border-bottom: 1px solid #efefef;
	}
	
	.search-list {
		display: flex;
		flex-wrap: wrap;
		.uni-tag {
			margin-top: 5px;
			margin-right: 10px;
		}
	}
}
</style>
